package com.jnmd.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jnmd.pojo.Speaker;
import com.jnmd.service.SpeakerService;

@Controller
public class SpeakerController {

	@Autowired
	private SpeakerService speakerService;
	
	@RequestMapping("speakerList.action")
	public String speakerList(Model model){
		
		List<Speaker> allSpeaker = speakerService.getAllSpeaker();
		model.addAttribute("allSpeaker", allSpeaker);
		return "admin/speaker/speakerManage";
	}
	
	@RequestMapping("addSpeakerView.action")
	public String addSpeakerView(){
		
		return "admin/speaker/addSpeaker";
	}
	
	@RequestMapping("addSpeaker")
	public String addSpeaker(Speaker speaker){
		if(null==speaker.getId()||speaker.getId()==0){
			speakerService.addSpeaker(speaker);
		}else{
			speakerService.updateSpeaker(speaker);
		}
		return "redirect:speakerList.action";
	}
	
	@RequestMapping("editSpeaker.action")
	public String editSpeaker(int id,Model model){
		
		Speaker speaker = speakerService.editSpeaker(id);
		model.addAttribute("speaker", speaker);
		return "admin/speaker/addSpeaker";
	}
	
	@RequestMapping("deleteSpeaker.action")
	@ResponseBody
	public String deleteSpeaker(int id){
		int deleteCount =  speakerService.deleteSpeaker(id);
		
		
		return "success";
	}
	
}
